//
// Created by 78472 on 2022/5/16.
//

#include "configParamUtil.h"
#include "qlibc/FileUtils.h"
#include <regex>
#include "log/Logging.h"
#include <utility>

configParamUtil* configParamUtil::instance = nullptr;

configParamUtil *configParamUtil::getInstance() {
    if(instance == nullptr)
        instance = new configParamUtil;
    return instance;
}

string configParamUtil::getconfigPath() {
    return dataDirPath;
}

void configParamUtil::setConfigPath(const string& configPath) {
    std::lock_guard<std::recursive_mutex> lg(mutex_);
    dataDirPath = configPath;
}


qlibc::QData configParamUtil::getPanelInfo(){
    std::lock_guard<std::recursive_mutex> lg(mutex_);
    if(panelInfoData.empty())
        panelInfoData.loadFromFile("/data/changhong/edge_midware/panelConfig.txt");
    return panelInfoData;
}

void configParamUtil::setPanelInfo(qlibc::QData& data){
    std::lock_guard<std::recursive_mutex> lg(mutex_);
    panelInfoData.setInitData(data);
    panelInfoData.saveToFile("/data/changhong/edge_midware/panelConfig.txt", true);
}

qlibc::QData configParamUtil::changePanelProperty(const qlibc::QData& panelData, bool& isPanelRemoved, bool& isPanelInfoChanged){
    std::lock_guard<std::recursive_mutex> lg(mutex_);
    if(panelInfoData.getString("device_mac") == panelData.getString("device_sn")){   //如果是当前面板
        string state = panelData.getString("state");
        if(state == "error"){
            //当前面板在APP上被删除，直接清空列表
            panelInfoData.setInitData(qlibc::QData());
            isPanelRemoved = true;
            isPanelInfoChanged = true;
        }else{
            if(panelInfoData.getString("phone") != panelData.getString("phone")){
                panelInfoData.setString("phone", panelData.getString("phone"));
                isPanelInfoChanged = true;
            }
            if(panelInfoData.asValue()["location"]["room_name"].asString() != panelData.getString("room_name")){
                panelInfoData.asValue()["location"]["room_name"] = panelData.getString("room_name");
                isPanelInfoChanged = true;
            }
            if(panelInfoData.asValue()["location"]["room_no"].asString() != panelData.getString("room_no")){
                panelInfoData.asValue()["location"]["room_no"] = panelData.getString("room_no");
                isPanelInfoChanged = true;
            }
            if(panelInfoData.getString("device_name") != panelData.getString("device_name")){
                panelInfoData.setString("device_name", panelData.getString("device_name"));
                isPanelInfoChanged = true;
            }
        }
        
        if(isPanelRemoved || isPanelInfoChanged){
             panelInfoData.saveToFile("/data/changhong/edge_midware/panelConfig.txt", true);
        }
        return panelInfoData;
    }
    return qlibc::QData();
}

void configParamUtil::saveHongmeiUnbindData(qlibc::QData &data)
{
    std::lock_guard<std::recursive_mutex> lg(mutex_);
    hongmeiUnbindData.setInitData(data);
    hongmeiUnbindData.saveToFile("/data/changhong/edge_midware/service_site_config/HongmeiUnbind.json", true);
}

void configParamUtil::saveHongmeiDestroyNetworkData(qlibc::QData &data)
{   
    std::lock_guard<std::recursive_mutex> lg(mutex_);
    hongmeiDestroyNetworkData.setInitData(data);
    hongmeiDestroyNetworkData.saveToFile("/data/changhong/edge_midware/service_site_config/HongmeiDestroyNetwork.json", true);
}
